/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2023. All rights reserved.
 */

package com.huawei.aiservice.utils;

import com.huawei.aiservice.constants.GatewayConstant;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;

/**
 * 响应工具类
 *
 * @since 2023/10/26 19:26
 */
@Component
public class ResponseUtil {
    /**
     * 封装响应
     *
     * @param response 响应
     * @param map 响应内容
     * @return Mono<Void> 过滤器链
     */
    public static Mono<Void> getUserResponse(ServerHttpResponse response, HashMap<String, String> map) {
        String resultJson = JsonUtil.toJson(map);
        response.getHeaders()
            .add(GatewayConstant.GitEEConstant.CONTENT_TYPE, GatewayConstant.GitEEConstant.CONTENT_TYPE_VALUE);
        byte[] bytes = resultJson.getBytes(StandardCharsets.UTF_8);
        DataBufferFactory bufferFactory = response.bufferFactory();
        DataBuffer buffer = bufferFactory.wrap(bytes);
        return response.writeWith(Flux.just(buffer));
    }
}
